package com.sl.au.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.sl.au.entity.scadaModelBaseInfo;

public interface ExdeScadaModelBaseInfoRepository extends JpaRepository<scadaModelBaseInfo, String>{

	@Query(" select s from scadaModelBaseInfo s where s.upParaId is null and (s.modelName like '%'+?1+'%' or s.memo like '%'+?1+'%') ")
	Page<scadaModelBaseInfo> getscadaModelBaseInfos(String key, Pageable pageable);

	@Query(" select s from scadaModelBaseInfo s where s.upParaId is null and (s.modelName like '%'+?1+'%' or s.memo like '%'+?1+'%') "
			+ "and s.user.saleArea.superior = ?2 ")
	Page<scadaModelBaseInfo> getscadaModelBaseInfos(String key, String superior, Pageable pageable);

	@Query(" select s from scadaModelBaseInfo s where s.upParaId is null and (s.modelName like '%'+?1+'%' or s.memo like '%'+?1+'%') "
			+ "and s.user.saleArea.superior = ?2 and s.user.saleArea.areaName = ?3 ")
	Page<scadaModelBaseInfo> getscadaModelBaseInfos(String key, String superior, String areaName, Pageable pageable);

	@Query(" select s from scadaModelBaseInfo s where s.upParaId is null and (s.modelName like '%'+?1+'%' or s.memo like '%'+?1+'%') "
			+ "and s.user = ?2 ")
	Page<scadaModelBaseInfo> getscadaModelBaseInfosByUser(String key, String userId, Pageable pageable);

	@Query(" select count(*) from scadaModelBaseInfo s where s.upParaId is null and (s.modelName like '%'+?1+'%' or s.memo like '%'+?1+'%') ")
	long getscadaModelBaseInfosCount(String key);

	@Query(" select count(*) from scadaModelBaseInfo s where s.upParaId is null and (s.modelName like '%'+?1+'%' or s.memo like '%'+?1+'%') "
			+ "and s.user.saleArea.superior = ?2 ")
	long getscadaModelBaseInfosCount(String key, String superior);

	@Query(" select count(*) from scadaModelBaseInfo s where s.upParaId is null and (s.modelName like '%'+?1+'%' or s.memo like '%'+?1+'%') "
			+ "and s.user.saleArea.superior = ?2 and s.user.saleArea.areaName = ?3 ")
	long getscadaModelBaseInfosCount(String key, String superior, String areaName);

	@Query(" select count(*) from scadaModelBaseInfo s where s.upParaId is null and (s.modelName like '%'+?1+'%' or s.memo like '%'+?1+'%') "
			+ "and s.user = ?2 ")
	long getscadaModelBaseInfosByUserCount(String key, String userId);

	@Query(" select s from scadaModelBaseInfo s where s.upParaId is null ")
	List<scadaModelBaseInfo> getRootNodes();

	@Query(" select s from scadaModelBaseInfo s where s.upParaId = ?1 ")
	List<scadaModelBaseInfo> findByupParaId(String id);
	
	@Query(" select s from scadaModelBaseInfo s where s.id in (?1) ")
	List<scadaModelBaseInfo> findByIds(String ids);
	
	@Query(" select s from scadaModelBaseInfo s where s.upParaId is not null ")
	List<scadaModelBaseInfo> findupParaId();

}
